﻿
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using WWLauncher.Constant;
using WWLauncher.Util;
using static WWLauncher.Util.GlobalHotKey;


namespace WWLauncher.ViewModel
{
    /// <summary>
    /// 程序设置
    /// </summary>
    [Serializable]
    public class AppConfig : INotifyPropertyChanged
    {
        #region 字段
        private SortType iconSortType = SortType.Custom; //图标排序类型
        private double menuCardWidth = Constants.MenuCardWidth;//菜单栏宽度

        private bool followMouse;  //面板跟随鼠标
        private AppHideType appHideType = AppHideType.StartExe;  //面板关闭方式 (默认启动程序后)
        private bool startedShowPanel = true;  //启动时是否显示主面板  默认显示
        [field: NonSerialized]
        private BitmapImage bitmapImage; //位图
        private string imagePath; //背景图片路径
        private string bacImgName = "系统默认";
        private int cardOpacity = 10;  //默认0.1的不透明度  此处显示数值 * 100
        private int bgOpacity = 100; // 背景图片不透明度 此处显示数值 * 100
        private int pannelOpacity = 100; //主面板不透明度 此处显示数值 * 100
        private int pannelCornerRadius = 8;  //面板圆角 默认8



        private string hotkeyStr = "Alt + Q";  //默认启动面板快捷键
        private HotkeyModifiers hotkeyModifiers = HotkeyModifiers.ModAlt; //默认启动面板快捷键
        private Key hotkey = Key.Q; //默认启动面板快捷键

        private string toDoHotkeyStr = "Ctrl + Shift + Q";  //待办任务快捷键
        private HotkeyModifiers toDoHotkeyModifiers; //待办任务快捷键
        private Key toDoHotkey = Key.Q; //待办任务快捷键


        private string colorPickerHotkeyStr = ""; //拾色器快捷键
        private HotkeyModifiers colorPickerHotkeyModifiers; //拾色器快捷键
        private Key colorPickerHotkey; //拾色器快捷键

        private string customIconUrl; //自定义图标url
        private string customIconJsonUrl;  //自定义图标json信息url

        private bool blurEffect; //毛玻璃效果 默认否
        private double blurValue;

        private UpdateType updateType = UpdateType.Gitee; //更新源 默认gitee源

        private bool selfStartUp = true; //开机自启动设置
        //private bool selfStartUped;  //是否已设置
        private bool pmModel; //性能模式

        private double imgPanelWidth = Constants.ImagePanelWidth;
        private double imgPanelHeight = Constants.ImagePanelHeight;
        private bool marginHide = true; //贴边隐藏
        private bool appAnimation = true; //主窗口动画效果
        private bool itemSpradeAnimation = true; //列表展开动画
        private int imageWidth = Constants.ImageWidth; //图片宽度
        private int imageHeight = Constants.ImageHeight; //图片高度

        private bool mouseMiddleShow;  //鼠标中键呼出 默认不启用

        private bool showBarIcon = true; //显示托盘图标  默认显示

        private bool doubleOpen; //双击打开项目  默认关闭

        private bool hoverMenu; //悬停切换菜单  默认关闭


        private bool enableAppHotKey = true;  //开启热键
        private bool enableTodoHotKey; //开启待办热键

        private bool enableColorPickerHotKey;  //新增 默认为false

        private SearchType searchType;

        private string menuPassword; //锁菜单密码

        private string passwordHint; //密码提示

        private bool? isShow;

        private bool lastPosition = true;//记住上次窗口位置

        private bool alwaysTopmost = true;//总是置顶
        private double windowLeft;
        private double windowTop;
        private bool isMonitor;
        private bool isShowMonitor = false;
        private bool isLogoVisible = true; // 标题logo是否显示
        private bool enablePowerManagement = true;

        #endregion
        public AppConfig()
        {
            this.ThemeConfig = new ThemeConfig();
            this.DebugConfig = new DebugConfig();
        }

        /// <summary>
        /// 总是置顶
        /// </summary>

        public bool AlwaysTopmost
        {
            get
            {
                return alwaysTopmost;
            }
            set
            {
                alwaysTopmost = value;
                OnPropertyChanged("AlwaysTopmost");
            }
        }


        #region GetSet

        /// <summary>
        /// 列表展开动画
        /// </summary>
        public bool ItemSpradeAnimation
        {
            get
            {
                return itemSpradeAnimation;
            }
            set
            {
                itemSpradeAnimation = value;
                OnPropertyChanged("ItemSpradeAnimation");
            }
        }
        /// <summary>
        /// 是否显示
        /// </summary>
        /// <remarks>true显示动画，false隐藏窗口，null显示窗口</remarks>
        public bool? IsShow
        {
            get
            {
                return isShow;
            }
            set
            {
                isShow = value;
                OnPropertyChanged("IsShow");
            }
        }
        /// <summary>
        /// 密码提示
        /// </summary>
        public string PasswordHint
        {
            get
            {
                return passwordHint;
            }
            set
            {
                passwordHint = value;
                OnPropertyChanged("PasswordHint");
            }
        }
        /// <summary>
        /// 锁菜单密码
        /// </summary>
        public string MenuPassword
        {
            get
            {
                return menuPassword;
            }
            set
            {
                menuPassword = value;
                OnPropertyChanged("MenuPassword");
            }
        }

        public SearchType SearchType
        {
            get
            {
                return searchType;
            }
            set
            {
                searchType = value;
                OnPropertyChanged("SearchType");
            }
        }

        public bool EnableColorPickerHotKey
        {
            get
            {
                return enableColorPickerHotKey;
            }
            set
            {
                enableColorPickerHotKey = value;
                OnPropertyChanged("EnableColorPickerHotKey");
            }
        }
        /// <summary>
        /// 列表展开动画
        /// </summary>
        public bool EnableAppHotKey
        {
            get
            {
                return enableAppHotKey;
            }
            set
            {
                enableAppHotKey = value;
                OnPropertyChanged("EnableAppHotKey");
            }
        }

        public bool EnableTodoHotKey
        {
            get
            {
                return enableTodoHotKey;
            }
            set
            {
                enableTodoHotKey = value;
                OnPropertyChanged("EnableTodoHotKey");
            }
        }

        public bool IsLogoVisible
        {
            get
            {
                return isLogoVisible;
            }
            set
            {
                isLogoVisible = value;
                OnPropertyChanged("IsLogoVisible");
            }
        }




        public bool HoverMenu
        {
            get
            {
                return hoverMenu;
            }
            set
            {
                hoverMenu = value;
                OnPropertyChanged("HoverMenu");
            }
        }
        /// <summary>
        /// 双击启动
        /// </summary>
        public bool DoubleOpen
        {
            get
            {
                return doubleOpen;
            }
            set
            {
                doubleOpen = value;
                OnPropertyChanged("DoubleOpen");
            }
        }
        /// <summary>
        /// 显示托盘图标
        /// </summary>
        public bool ShowBarIcon
        {
            get
            {
                return showBarIcon;
            }
            set
            {
                showBarIcon = value;
                OnPropertyChanged("ShowBarIcon");
            }
        }
        /// <summary>
        /// 鼠标中键呼出
        /// </summary>
        public bool MouseMiddleShow
        {
            get
            {
                return mouseMiddleShow;
            }
            set
            {
                mouseMiddleShow = value;
                OnPropertyChanged("MouseMiddleShow");
            }
        }

        public int ImageWidth
        {
            get
            {
                return imageWidth;
            }
            set
            {
                imageWidth = value;
                //同时设置高度
                ImageHeight = value;


                //计算 容器宽度因子
                double i = ((double)imageWidth - Constants.ImageWidth) / 5d;
                double s = 2.44;
                i *= 2d;
                while (i > 1)
                {
                    i /= 10d;
                }

                if (i > 0d)
                {
                    s -= i;
                }
                if (s < 2.2)
                {
                    s = 2.2;
                }
                //设置容器宽度
                ImgPanelWidth = ImageWidth * s;

                OnPropertyChanged("ImageWidth");
            }
        }

        public int ImageHeight
        {
            get
            {
                return imageHeight;
            }
            set
            {
                imageHeight = value;

                //计算容器高度因子
                double i = ((double)imageHeight - Constants.ImageHeight) / 5d;
                while (i > 1)
                {
                    i /= 10d;
                }
                double s = 2.00;
                if (i > 0d)
                {
                    s -= i;
                }
                if (s < 1.5) s = 1.5;

                //设置容器高度
                ImgPanelHeight = ImageHeight * s;
                OnPropertyChanged("ImageHeight");
            }
        }

        public bool AppAnimation
        {
            get
            {
                return appAnimation;
            }
            set
            {
                appAnimation = value;
                OnPropertyChanged("AppAnimation");
            }
        }
        /// <summary>
        /// 贴边隐藏
        /// </summary>
        public bool MarginHide
        {
            get
            {
                return marginHide;
            }
            set
            {
                marginHide = value;
                OnPropertyChanged("MarginHide");
            }
        }
        /// <summary>
        /// 记住上次窗口位置
        /// </summary>
        public bool LastPosition
        {
            get
            {
                return lastPosition;
            }
            set
            {
                lastPosition = value;
                OnPropertyChanged("LastPosition");
            }
        }

        /// <summary>
        /// 设置容器宽度
        /// </summary>
        [JsonIgnore]
        [IgnoreDataMember]
        public double ImgPanelWidth
        {
            get
            {
                return imgPanelWidth;
            }
            set
            {
                imgPanelWidth = value;
                OnPropertyChanged("ImgPanelWidth");
            }
        }
        /// <summary>
        /// 设置容器高度
        /// </summary>
        [JsonIgnore]
        [IgnoreDataMember]
        public double ImgPanelHeight
        {
            get
            {
                return imgPanelHeight;
            }
            set
            {
                imgPanelHeight = value;
                OnPropertyChanged("ImgPanelHeight");
            }
        }

        /// <summary>
        /// 性能模式
        /// </summary>
        public bool PMModel
        {
            get
            {
                return pmModel;
            }
            set
            {
                pmModel = value;
                OnPropertyChanged("PMModel");
            }
        }
        ///// <summary>
        ///// 开机自启动设置
        ///// </summary>
        //public bool SelfStartUped
        //{
        //    get
        //    {
        //        return selfStartUped;
        //    }
        //    set
        //    {
        //        selfStartUped = value;
        //        OnPropertyChanged("SelfStartUped");
        //    }
        //}
        /// <summary>
        /// 开机自启动设置
        /// </summary>
        public bool SelfStartUp
        {
            get
            {
                return selfStartUp;
            }
            set
            {
                selfStartUp = value;
                //selfStartUped = true;
                OnPropertyChanged("SelfStartUp");
            }
        }

        public Key ColorPickerHotkey
        {
            get
            {
                return colorPickerHotkey;
            }
            set
            {
                colorPickerHotkey = value;
                OnPropertyChanged("ColorPickerHotkey");
            }
        }


        public HotkeyModifiers ColorPickerHotkeyModifiers
        {
            get
            {
                return colorPickerHotkeyModifiers;
            }
            set
            {
                colorPickerHotkeyModifiers = value;
                OnPropertyChanged("ColorPickerHotkeyModifiers");
            }
        }

        public string ColorPickerHotkeyStr
        {
            get
            {
                return colorPickerHotkeyStr;
            }
            set
            {
                colorPickerHotkeyStr = value;
                OnPropertyChanged("ColorPickerHotkeyStr");
            }
        }


        public Key ToDoHotkey
        {
            get
            {
                //兼容老版本
                if (toDoHotkey == Key.None)
                {
                    toDoHotkey = Key.E;
                }
                return toDoHotkey;
            }
            set
            {
                toDoHotkey = value;
                OnPropertyChanged("ToDoHotkey");
            }
        }


        public HotkeyModifiers ToDoHotkeyModifiers
        {
            get
            {
                if (toDoHotkeyModifiers == 0)
                {
                    toDoHotkeyModifiers = HotkeyModifiers.ModControl | HotkeyModifiers.ModShift;
                }
                return toDoHotkeyModifiers;
            }
            set
            {
                toDoHotkeyModifiers = value;
                OnPropertyChanged("ToDoHotkeyModifiers");
            }
        }

        public string ToDoHotkeyStr
        {
            get
            {
                //兼容老版本
                if (toDoHotkeyStr == null)
                {
                    toDoHotkeyStr = "Ctrl + Shift + Q";
                }
                return toDoHotkeyStr;
            }
            set
            {
                toDoHotkeyStr = value;
                OnPropertyChanged("ToDoHotkeyStr");
            }
        }

        public UpdateType UpdateType
        {
            get
            {
                return updateType;
            }
            set
            {
                updateType = value;
                OnPropertyChanged("UpdateType");
            }
        }

        public double BlurValue
        {
            get
            {
                return blurValue;
            }
            set
            {
                blurValue = value;
                OnPropertyChanged("BlurValue");
            }
        }

        public bool BlurEffect
        {
            get
            {
                return blurEffect;
            }
            set
            {
                blurEffect = value;
                if (blurEffect)
                {
                    BlurValue = 100;
                }
                else
                {
                    BlurValue = 0;
                }
                OnPropertyChanged("BlurEffect");
            }
        }
        public string CustomIconUrl
        {
            get
            {
                return customIconUrl;
            }
            set
            {
                customIconUrl = value;
                OnPropertyChanged("CustomIconUrl");
            }
        }
        public string CustomIconJsonUrl
        {
            get
            {
                return customIconJsonUrl;
            }
            set
            {
                customIconJsonUrl = value;
                OnPropertyChanged("CustomIconJsonUrl");
            }
        }
        public Key Hotkey
        {
            get
            {
                return hotkey;
            }
            set
            {
                hotkey = value;
                OnPropertyChanged("Hotkey");
            }
        }
        public string HotkeyStr
        {
            get
            {
                return hotkeyStr;
            }
            set
            {
                hotkeyStr = value;
                OnPropertyChanged("HotkeyStr");
            }
        }
        public HotkeyModifiers HotkeyModifiers
        {
            get
            {
                return hotkeyModifiers;
            }
            set
            {
                hotkeyModifiers = value;
                OnPropertyChanged("HotkeyModifiers");
            }
        }



        public int PannelCornerRadius
        {
            get
            {
                return pannelCornerRadius;
            }
            set
            {
                pannelCornerRadius = value;
                OnPropertyChanged("pannelCornerRadius");
            }
        }

        public int PannelOpacity
        {
            get
            {
                return pannelOpacity;
            }
            set
            {
                pannelOpacity = value;
                OnPropertyChanged("PannelOpacity");
            }
        }
        public int BgOpacity
        {
            get
            {
                return bgOpacity;
            }
            set
            {
                bgOpacity = value;
                OnPropertyChanged("BgOpacity");
            }
        }

        public int CardOpacity
        {
            get
            {
                return cardOpacity;
            }
            set
            {
                cardOpacity = value;
                OnPropertyChanged("CardOpacity");
            }
        }

        public string BacImgName
        {
            get
            {
                return bacImgName;
            }
            set
            {
                bacImgName = value;
                OnPropertyChanged("BacImgName");
            }
        }
        /// <summary>
        /// 存储图片路径
        /// </summary>
        public string ImagePath
        {
            get
            {
                return imagePath;
            }
            set
            {
                imagePath = value;
                OnPropertyChanged("ImagePath");
            }
        }

        [JsonIgnore]
        [IgnoreDataMember]
        public BitmapImage BitmapImage
        {
            get
            {
                if (string.IsNullOrEmpty(imagePath))
                {
                    bacImgName = "系统默认";
                    bitmapImage = ImageUtil.GetBitmapIconByUri(Constants.DefaultBacImage);
                }
                else
                {
                    bitmapImage = ImageUtil.GetBitmapIconByUri(ImagePath);
                }
                return bitmapImage;
            }
            set
            {
                bitmapImage = value;
                //imagePath = ImageUtil.BitmapImagePath(bitmapImage);
            }
        }

        /// <summary>
        /// 启动时是否显示主面板
        /// </summary>
        public bool StartedShowPanel
        {
            get
            {
                return startedShowPanel;
            }
            set
            {
                startedShowPanel = value;
                OnPropertyChanged("StartedShowPanel");
            }
        }
        /// <summary>
        /// 面板隐藏方式
        /// </summary>
        public AppHideType AppHideType
        {
            get
            {
                return appHideType;
            }
            set
            {
                appHideType = value;
                OnPropertyChanged("AppHideType");
            }
        }


        /// <summary>
        /// 跟随鼠标
        /// </summary>
        public bool FollowMouse
        {
            get
            {
                return followMouse;
            }
            set
            {
                followMouse = value;
                OnPropertyChanged("FollowMouse");
            }
        }

        /// <summary>
        /// 图标排序类型
        /// </summary>
        public SortType IconSortType
        {
            get
            {
                return iconSortType;
            }
            set
            {
                iconSortType = value;
                OnPropertyChanged("IconSortType");
            }
        }
        /// <summary>
        /// 窗口宽度
        /// </summary>
        public double WindowWidth
        {
            get
            {
                return 720;
            }
        }
        /// <summary>
        /// 窗口高度
        /// </summary>
        public double WindowHeight
        {
            get
            {
                return 440;
            }
        }
        /// <summary>
        /// 窗口Top位置
        /// </summary>
        public double WindowTop
        {
            get
            {
                return windowTop;
            }
            set
            {
                windowTop = value;
                OnPropertyChanged("WindowTop");
            }
        }
        /// <summary>
        /// 窗口Left位置
        /// </summary>
        public double WindowLeft
        {
            get
            {
                return windowLeft;
            }
            set
            {
                windowLeft = value;
                OnPropertyChanged("WindowLeft");
            }
        }
        /// <summary>
        /// 菜单栏宽度
        /// </summary>
        public double MenuCardWidth
        {
            get
            {
                return menuCardWidth;
            }
            set
            {
                menuCardWidth = value;
                OnPropertyChanged("MenuCardWidth");
            }
        }
        /// <summary>
        /// 监控硬件
        /// </summary>
        public bool IsMonitor
        {
            get => isMonitor;
            set
            {
                isMonitor = value;
                OnPropertyChanged("IsMonitor");
            }
        }
        /// <summary>
        /// 是否显示监控悬浮窗口
        /// </summary>
        public bool IsShowMonitor
        {
            get => isShowMonitor;
            set
            {
                isShowMonitor = value;
                OnPropertyChanged("IsShowMonitor");
            }
        }
        public ThemeConfig ThemeConfig { set; get; }

        public DebugConfig DebugConfig { get; set; }

        public bool EnablePowerManagement
        {
            get => enablePowerManagement;
            set
            {
                enablePowerManagement = value;
                OnPropertyChanged("EnablePowerManagement");
            }
        }


        [field: NonSerialized()]
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
